package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanUpdateDto;
import com.zzyl.dto.ProjectPlanRequestDto;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingPlanVo2;
import com.zzyl.vo.NursingPlanVo3;
import com.zzyl.vo.ProjectPlanVo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    NursingPlanMapper nursingPlanMapper;
    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public List<NursingPlanVo2> getAllPlan() {
        return nursingPlanMapper.getAllPlan();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNursingPlan(ProjectPlanRequestDto projectPlanRequestDto) {
        //这边要插入两张表1第一张是计划表，是有一张是他和项目的关联表，根据传入的id来给关联表中插入一条或多条数据

        //判断是否重名，如果传如数据存在重名则抛出异常
        if (nursingPlanMapper.getById(projectPlanRequestDto.getId()) != null) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_ADD_FAIL_RENAME);
        }
        nursingPlanMapper.addNursingPlan(projectPlanRequestDto);
        // 获取新生成的 planId
        Long planId = projectPlanRequestDto.getId();

        // 设置 planId 到每个 projectPlan 对象中
        projectPlanRequestDto.getProjectPlans().forEach(projectPlan -> {
            projectPlan.setPlanId(planId);
        });
        for (ProjectPlanRequestDto.ProjectPlan projectPlan : projectPlanRequestDto.getProjectPlans()) {
            nursingProjectPlanMapper.addNursingProjectPlan(projectPlan);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResponse<NursingPlanVo3> getByPagePlan(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo3> nursingProjects = (Page<NursingPlanVo3>) nursingPlanMapper.selectByPagePlan(name, status);
        PageResponse<NursingPlanVo3> projectVoPageResponse = PageResponse.of(nursingProjects, NursingPlanVo3.class);


        return projectVoPageResponse;
    }

    @Override
    public NursingPlanVo getById(Long id) {
        //异常处理，如果没查到数据抛出异常id不存在
        if (nursingPlanMapper.getById(id) == null) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_SELECT_NULL);
        }
        return nursingPlanMapper.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNursingPlan(Long id, NursingPlanUpdateDto nursingPlanUpdateDto) {
        if (nursingPlanMapper.getById(id) == null) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_SELECT_NULL);
        }
        if (nursingPlanMapper.uuuu(id) > 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_UPDATE_FAIL_RENAME);
        }
        nursingPlanUpdateDto.setId(id);
        //先修改计划表中的基础字段
        nursingPlanMapper.updateNursingPlan(nursingPlanUpdateDto);

        //在修改关联表中的数据
        //list中可能有多条数据，逐一修改
        nursingPlanUpdateDto.getProjectPlans().forEach(projectPlan -> {
            nursingProjectPlanMapper.updateNursingProjectPlan(projectPlan);
        });

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        //这里删除护理计划要同时删除两张表的数据一张是计划表还有一张是关联表
        nursingPlanMapper.deleteById(id);
        nursingProjectPlanMapper.deleteById(id);
    }

    @Override
    public void ooo(Long id, Integer status) {
        if (status == 0) {
            //禁用
            //判断项目是否被关联，如果被关联，则提示项目已与护理计划绑定，不可删除或编辑
            int count = nursingProjectPlanMapper.slectByPlanId(id);
            if (count > 0) {
                throw new BaseException(BasicEnum.NURSING_PROJECT_BIND);
            }
        }
        nursingPlanMapper.ooo(id, status);
//        NursingPlanUpdateDto nursingPlanUpdateDto = new NursingPlanUpdateDto();
//        nursingPlanUpdateDto.setId(id);
//        nursingPlanUpdateDto.setStatus(status);
//        nursingPlanMapper.updateNursingPlan(nursingPlanUpdateDto);

    }

}
